package com.black.web.anonymous;

import com.black.common.constant.CacheConstants;
import com.black.common.core.domain.R;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 图片验证码
 *
 * @author L
 */
@Api(tags = "匿名访问-验证码")
@RestController
@RequestMapping("/anonymous/captcha")
public class EasyCaptchaController {

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "png格式验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "width", value = "验证码图片宽度", defaultValue = "150", dataType = "int"),
            @ApiImplicitParam(name = "height", value = "验证码图片高度", defaultValue = "50", dataType = "int"),
            @ApiImplicitParam(name = "len", value = "验证码位数", defaultValue = "4", dataType = "int")
    })
    @GetMapping("/png")
    public R<Map<String, String>> captchaPng(@RequestParam(name = "width", defaultValue = "150") int width,
                                             @RequestParam(name = "height", defaultValue = "50") int height,
                                             @RequestParam(name = "len", defaultValue = "4") int len) {

        SpecCaptcha specCaptcha = new SpecCaptcha(width, height, len);
//        验证码文本
        String verCode = specCaptcha.text().toLowerCase();
//        得到验证码图片，转成base64
        String base64 = specCaptcha.toBase64();
//        验证码唯一标识
        String key = UUID.randomUUID().toString();

        Map<String, String> res = new HashMap<>();
        res.put("img", base64);
        res.put("uuid", key);
        // 存入redis并设置过期时间为5分钟
        String redisKey = CacheConstants.CAPTCHA_CODE_KEY + key;
        this.redisTemplate.opsForValue().set(redisKey, verCode, 5, TimeUnit.MINUTES);
        // 将key和base64返回给前端
        return R.ok(res);
    }

    @ApiOperation(value = "算术验证码")
    @GetMapping("/arithmetic")
    public R<Map<String, String>> captchaArithmetic(@RequestParam(name = "width", defaultValue = "150") int width,
                                                    @RequestParam(name = "height", defaultValue = "50") int height,
                                                    @RequestParam(name = "len", defaultValue = "2") int len) {

        ArithmeticCaptcha captcha = new ArithmeticCaptcha(width, height, len);
//        验证码文本
        String verCode = captcha.text().toLowerCase();
//        得到验证码图片，转成base64
        String base64 = captcha.toBase64();
//        验证码唯一标识
        String key = UUID.randomUUID().toString();

        Map<String, String> res = new HashMap<>();
        res.put("img", base64);
        res.put("uuid", key);
        // 存入redis并设置过期时间为5分钟
        String redisKey = CacheConstants.CAPTCHA_CODE_KEY + key;
        this.redisTemplate.opsForValue().set(redisKey, verCode, 5, TimeUnit.MINUTES);
        // 将key和base64返回给前端
        return R.ok(res);
    }
}
